package tako.wisers.com.android.apis.location;

import java.util.List;

import tako.wisers.com.android.apis.R;
import tako.wisers.com.android.apis.activity.Tako;
import tako.wisers.com.android.apis.adapter.ListManager;
import tako.wisers.com.android.apis.item.BaseItem;
import tako.wisers.com.android.apis.parser.xml.item.ButtonItem;
import tako.wisers.com.android.apis.util.Global;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Canvas;
import android.graphics.Point;
import android.graphics.drawable.Drawable;
import android.location.Location;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;
import com.google.android.maps.MyLocationOverlay;
import com.google.android.maps.Overlay;
import com.google.android.maps.Projection;

public class OverlayControl {
	private MapView mMapView;

	private Drawable mMarker;

	private int mMarkerXOffset;

	private int mMarkerYOffset;

	private Activity mActivity;

	private Projection mProjection;

	private List<Overlay> overlays;

	public OverlayControl(MapView mapView) {
		mMapView = mapView;
		mProjection = mapView.getProjection();
		overlays = mMapView.getOverlays();
	}

	public void build(Activity activity) {
		// Make sure to give mMarker bounds so it will draw in the overlay
		mMarker = activity.getResources().getDrawable(R.drawable.map_pin);
		final int intrinsicWidth = mMarker.getIntrinsicWidth();
		final int intrinsicHeight = mMarker.getIntrinsicHeight();
		mMarker.setBounds(0, 0, intrinsicWidth, intrinsicHeight);

		mMarkerXOffset = -(intrinsicWidth / 2);
		mMarkerYOffset = -intrinsicHeight;
		mActivity = activity;
	}

	public void setOverlay(Location location, int position) {
		overlays.add(new PanoramioOverlay(location, position));
	}

	public void setOverlay(GeoPoint point, int position) {
		overlays.add(new PanoramioOverlay(point, position));
	}
	
	public void setOverlay(Overlay overlay) {
		overlays.add(overlay);
	}

	public void setMyLocationOverlay(Context context) {
		MyLocationOverlay mMyLocationOverlay = new MyLocationOverlay(context,
				mMapView);
		overlays.add(mMyLocationOverlay);
	}

	public void Invalidate() {
		mMapView.invalidate();
	}

	public void setZoom(int zoom) {
		mMapView.getController().setZoom(zoom);
	}

	public class PanoramioOverlay extends Overlay {
		private GeoPoint mPoint;
		private int position;
//		private Drawable marker;

		public PanoramioOverlay(Location location, int position) {
			int lat = (int) (location.getLatitude() * 1E6);
			int lon = (int) (location.getLongitude() * 1E6);
			mPoint = new GeoPoint(lat, lon);
			this.position = position;
//			this.marker = mMarker;
		}

		public PanoramioOverlay(GeoPoint point, int position) {
			mPoint = point;
			this.position = position;
//			this.marker = mMarker;
		}
		
		@Override
		public boolean onTouchEvent(MotionEvent e, MapView mapView) {
			int action = e.getAction();
			if(action == MotionEvent.ACTION_MOVE){
				return super.onTouchEvent(e, mapView);
			}
			if(action == MotionEvent.ACTION_UP){
				System.gc();
				return true;
			}
			if (!(mActivity instanceof Tako) || action != MotionEvent.ACTION_DOWN){
				return super.onTouchEvent(e, mapView);
			}

			if (position == -1 || !isMark(e))
				return false;
			Tako tako = (Tako) mActivity;
			ListManager lm = ListManager.getInstance(mActivity);
			BaseItem item = lm.get(position);

			if (item == null)
				return false;
			Intent intent = new Intent();
			tako.setClass(item, intent);
			intent.putExtra(Global.ITEM_KEY, item);
			tako.startActivity(intent);
			return true;
		}

		private boolean isMark(MotionEvent e) {
			Point point = new Point();
			mProjection.toPixels(mPoint, point);

			float x1 = point.x;
			float y1 = point.y;

			float x = e.getX();
			float y = e.getY();

			boolean inMark = (x > (x1 + mMarkerXOffset) && x < (x1 - mMarkerXOffset))
					&& (y > (y1 + mMarkerYOffset) && y < y1);
			return inMark;
		}

		public void draw(Canvas canvas, MapView mapView, boolean shadow) {
			Point point = new Point();
			mProjection.toPixels(mPoint, point);
			drawAt(canvas, mMarker, point.x + mMarkerXOffset, point.y
					+ mMarkerYOffset, shadow);
		}
	}

	public void clearOverlay() {
		overlays.clear();
	}
	
	public int getZoom() {
		int zoomLevel = mMapView.getZoomLevel();
		if (zoomLevel > 16)
			zoomLevel = 15;
		return zoomLevel;
	}

	public Drawable getMarker() {
		return mMarker;
	}

	public int getMarkerXOffset() {
		return mMarkerXOffset;
	}

	public int getMarkerYOffset() {
		return mMarkerYOffset;
	}
	

}
